//
//  MyReplyVC.m
//  TuYouAPP
//
//  Created by L on 14-2-27.
//
//

#import "MyReplyVC.h"
#import "MyReplyCell.h"
#import "UIImageView+WebCache.h"
#import "DynamicReplyVC.h"
@interface MyReplyVC ()
{
    NSMutableArray  *tempArray;
}
@end


@implementation MyReplyVC
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)loadData
{
    [SVProgressHUD showWithStatus:@"加载中"];
    self.page = 1;
    [[ TYUser currentUser]getMyCommentNewState:self.page AnyCall:^(NSArray *array, NSString *eror) {
        [self.slimeView endRefresh];
        self.tableview.tableFooterView.hidden = NO;
        if (!eror && array) {
            [SVProgressHUD dismiss];

            self.page++;
            tempArray = [[NSMutableArray alloc]init];
            [tempArray addObjectsFromArray:array];
            [self.tableview reloadData];
            if (array.count<ONE_PAGE) {
                self.refreshLabel.text = self.textNoMore;
            }
            else
            {
                self.refreshLabel.text = self.textPull;

            }
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:eror];
        }
        
    }];
}
- (void)viewDidLoad
{
    self.mPageName = @"我的参与回复";
    [super viewDidLoad];
    [self loadData];

    
	// Do any additional setup after loading the view.
    self.Title = @"我参与的回复";
    self.LeftBarTitle = @"我的";
    self.haveFullupRefreshView = YES;

    [self initWithRefreshTableViewIos7:CGRectMake(0, 64, 320, DEVICE_Height-64) Ios6rect:CGRectMake(0, 44, 320, DEVICE_Height-64)];
    self.tableview.tableFooterView.hidden = YES;

    self.tableview.backgroundColor = ColorRGB(230, 230, 230);
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableview];

    
    [[TYUser currentUser] clearFlag:E_flag_have_reback];
    
}
-(void)leftBtnTouched:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tempArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"MyReplyCell";
    
    MyReplyCell *cell = (MyReplyCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    
    BOOL isUsed = NO;
    if (!isUsed &&! cell) {
        UINib *nib = [UINib nibWithNibName:@"MyReplyCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:@"MyReplyCell"];
        // isUsed = YES;
        cell = (MyReplyCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
        
    }
    
    TYUserState *data = [tempArray objectAtIndex:indexPath.row];
    NSMutableString *first = [[NSMutableString alloc]initWithFormat:@"@%@:",data.mNewestComment.mUserName ];
    [first appendString:data.mNewestComment.mText];
   [cell upDateCell:first content:data.mContext];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:first];
    
    [str addAttribute:NSForegroundColorAttributeName value:ColorRGB(205, 136, 52) range:NSMakeRange(0,data.mNewestComment.mUserName.length+1)];

    NSInteger strLength = [[str string] length];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:2];
    
    [str addAttribute:NSParagraphStyleAttributeName
                      value:style
                      range:NSMakeRange(0, strLength)];
    
  //  cell.publishContent.attributedText = attString;

    
    cell.firstReply.attributedText = str;
    
    cell.myView.layer.borderWidth = 0.5;
    cell.myView.layer.borderColor = [ColorRGB(212, 212, 212)CGColor];
    
    cell.contentName.text = data.mTaguser.mUserDisplayName;
    NSString *pingluncount = data.mReBackCount!=0? [NSString stringWithFormat:@"%d条回复",data.mReBackCount]:@"暂无回复";
    cell.pinglunCount.text = pingluncount;
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:data.mContext];
    NSInteger astrLength = [data.mContext length];
    NSMutableParagraphStyle *astyle = [[NSMutableParagraphStyle alloc] init];
    [astyle setLineSpacing:2];
    [attString addAttribute:NSParagraphStyleAttributeName
                      value:astyle
                      range:NSMakeRange(0, astrLength)];

    cell.content.attributedText = attString;
    cell.replyTime.text = data.mNewestComment.mTime;
    [cell.headImage setImageWithURL:[NSURL URLWithString:data.mTaguser.mHeadImgURL] placeholderImage:[UIImage imageNamed:@"70_70.png"]];
  //  cell.firstReply.text  = data.myReply;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // cell.textLabel.text = @"success";
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height+1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TYUserState *state = [tempArray objectAtIndex:indexPath.row];
    DynamicReplyVC *vc = [[DynamicReplyVC alloc]init];
    vc.thePublish = state;
    [self.navigationController pushViewController:vc animated:YES];
  
    
}
-(void)slimeRefreshStartRefresh:(SRRefreshView *)refreshView
{
    [self loadData];

}
-(void)refresh
{
 
    [[ TYUser currentUser]getMyCommentNewState:self.page AnyCall:^(NSArray *array, NSString *eror) {
        if (tempArray.count>0) {
          //  [SVProgressHUD showErrorWithStatus:@"没有更多数据"];
            self.refreshLabel.text = self.textNoMore;
            [self stopLoading];
            return;
        }
    [self stopLoading];
    [SVProgressHUD dismiss];
    self.tableview.tableFooterView.hidden = NO;
    
    if (!eror && array) {
        self.page++;
        if (array.count == 0) {
         //   [SVProgressHUD showErrorWithStatus:@"没有更多数据"];
            self.refreshLabel.text = self.textNoMore;

            return ;
        }
      //  tempArray = [[NSMutableArray alloc]init];
        [tempArray addObjectsFromArray:array];
        [self.tableview reloadData];
        if (array.count <ONE_PAGE) {
            self.refreshLabel.text = self.textNoMore;
        }
        else
        {
            self.refreshLabel.text = self.textPull;
            
        }

    }
    else
    {
        [SVProgressHUD showErrorWithStatus:eror];
    }
    
}];
   }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
